package sanrenxing.dayEnd.backRepertoryTotal.service.impl;

import org.springframework.stereotype.Service;
import sanrenxing.dayEnd.backRepertoryTotal.dao.FinancialsecuritiesDao;
import sanrenxing.dayEnd.backRepertoryTotal.dao.SecuritiesInformationDao;
import sanrenxing.dayEnd.backRepertoryTotal.domain.SecuritiesInformation;
import sanrenxing.dayEnd.backRepertoryTotal.service.SecuritiesInformationBiz;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * 功能：债券业务层实现类
 * 编写者：蒋玲苹
 * 日期：210318
 * 版本：1.0
 */
@Service
public class SecuritiesInformationBizImpl implements SecuritiesInformationBiz {
    @Resource(name = "securitiesInformationDao")
    private SecuritiesInformationDao securitiesInformationDao;
    //引入证券dao对象
    @Resource(name = "financialsecuritiesDao")
    private FinancialsecuritiesDao financialsecuritiesDao;

    /**
     * 添加数据
     *
     * @param securitiesInformation
     * @return 0:添加失败,1:添加成功
     */
    @Override
    public int add(SecuritiesInformation securitiesInformation) {
        return securitiesInformationDao.add(securitiesInformation);
    }

    /**
     * 删除数据
     * @param bondId
     * @return 删除影响的行数(0 : 删除失败, 1 : 删除成功)
     */
    //@Transactional(rollbackFor = Exception.class)
    @Override
    public int delete(String bondId) {
        //判断是否为批量删除
        if(bondId.contains(",")){
            //对编号按“，”分割
            String[] strs=bondId.split(",");
            //循环删除
            for(String str:strs){
                //调用方法删除债券
                securitiesInformationDao.delete(str);
                //调用删除证券的方法
                financialsecuritiesDao.delete(str);
                //securitiesInformationDao.deletes(str);
            }
            //返回1
            return 1;
        }
        //调用删除证券的方法
        financialsecuritiesDao.delete(bondId);
        //securitiesInformationDao.deletes(bondId);
        //调用删除债券的方法
        return securitiesInformationDao.delete(bondId);
    }

    /**
     * 修改数据
     * @param securitiesInformation
     * @return 0:修改失败,1:修改成功
     */
    @Override
    public int update(SecuritiesInformation securitiesInformation) {
        return securitiesInformationDao.update(securitiesInformation);
    }

    /**
     * 分页查询
     *
     * @param securitiesInformation
     * @param row
     * @param page
     * @return
     */
    @Override
    public List<SecuritiesInformation> queryAll(SecuritiesInformation securitiesInformation, int row, int page) {
        HashMap<String,Object> map=new HashMap<>();
        map.put("securitiesInformation",securitiesInformation);
        map.put("start",page);
        map.put("end",(page+row));
        return securitiesInformationDao.queryAll(map);
    }

    /**
     * 统计记录数
     *
     * @param securitiesInformation
     * @return 记录行数据
     */
    @Override
    public int count(SecuritiesInformation securitiesInformation) {
        return securitiesInformationDao.count();
    }

    /**
     * 添加证券信息数据
     * @param securitiesInformation
     */
    @Override
    public void addSec(SecuritiesInformation securitiesInformation) {
        //债券编号
        String bondId=securitiesInformation.getBondId();
        //债券名称
        String bondName=securitiesInformation.getBondName();
        //计息起始日
        String beginInte=securitiesInformation.getBeginInte();
        //延迟日期：这里拿起始日期加一天

        //调用添加证券信息方法
        securitiesInformationDao.addSec(bondId,bondName,beginInte,beginInte);
    }


}
